package main

import (
	"fmt"
)

/*
  适配器模式
  例子：AudioPlayer 使用适配器类 MediaAdapter 传递所需的音频类型，播放各种格式。
*/

// 高级播放器接口
type HightPlayer interface {
	playMp3(string)
	playMp4(string)
}

// mp3高级播放器
type Mp3Player struct {
}
func (m Mp3Player) playMp3(filename string)  {
	fmt.Println("Mp3Player play ",filename)
}
func (m Mp3Player) playMp4(filename string)  {
	//不做处理
}
// mp4高级播放器
type Mp4Player struct {
}
func (m Mp4Player) playMp3(filename string)  {
	//不做处理
}
func (m Mp4Player) playMp4(filename string)  {
	fmt.Println("Mp4Player play ",filename)
}

// 媒体播放器
type MediaPlayer interface {
	play(string,string)
}
// 媒体播放器适配器
type MediaAdapter struct {
	player HightPlayer
}

func (a MediaAdapter) play(fileType, fileName string) {
	if fileType == "mp3" {
		a.player.playMp3(fileName)
	} else if fileType == "mp4"{
		a.player.playMp4(fileName)
	}
}

func NewMediaAdapter(fileType string) MediaAdapter {
	var player HightPlayer
 	if fileType == "mp3" {
		player = Mp3Player{}
	} else if fileType == "mp4"{
		player = Mp4Player{}
	}
	return MediaAdapter{player:player}
}

type AudioPlayer struct {
	adapter MediaAdapter
}


func (a AudioPlayer) play(fileType, fileName string) {
	a.adapter = NewMediaAdapter(fileType)
	a.adapter.play(fileType,fileName)
}

func main() {
   player := AudioPlayer{}
   player.play("mp3","beyond the horizon.mp3")
   player.play("mp4","alone.mp4")
}